# -*- coding: utf-8 -*-
'''
Created on Mar 13, 2013

@author: LONG HOANG GIANG
'''
import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import sqlite3
import gzip as _gzip 
import json
from CrawlerLib2 import commonlib
from Model.storymodelv3 import StoryModel
from Cheetah.Template import Template
import zlib

template = '''\
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta charset="UTF-8" />
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
    {detail}
</body>
</html>
'''


def gzip(fileName, data):
    gz = _gzip.GzipFile(filename=fileName, mode='wb', compresslevel=6)
    gz.write(data)
    gz.close()
    
def gzipfile(fileInput, fileOut):
    
    fin = open(fileInput, 'rb')
    fout = _gzip.open(fileOut, 'wb')
    fout.writelines(fin)
    fout.close()
    fin.close()
    os.unlink(fileInput)

def convertDbToJson(database, output):
    if output[-1] != '/': output += '/'
    if not os.path.isdir(output):
        os.makedirs(output, 0777)
    
    connection = sqlite3.connect(database)
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM vn_gq_udv_story")
    data = cursor.fetchall()
    jsData = []
    for item in data:
        title = item[2]
        detail = item[3]
        contentId = commonlib.crc32unsigned(title)
        fileName = output + contentId
        gzip(fileName, detail)
        jsData.append({'title': title, 'id': contentId})
    cursor.close()
    fileName = output + 'data'
    gzip(fileName, json.dumps(jsData))
    
def convertNewDatabase(database, output):
    
    if output[-1] != '/': output += '/'
    if not os.path.isdir(output):
        os.makedirs(output, 0777)
    if not os.path.isdir(output + 'files/'):
        os.makedirs(output + 'files/', 0777)
        
    connection = sqlite3.connect(database)
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM vn_gq_udv_story")
    data = cursor.fetchall()
    
    smodel = StoryModel(output + 'data')
    smodel.open(True)
    for item in data:
        
        catid = item[1]
        title = item[2]
#        tree = commonlib.buildTreeFromHtml(item[3])
#        body = tree.xpath("//body/*")
#        if len(body) > 0:
#            body = commonlib.Etree.tostring(body[0])
#        else:
#            body = item[3]
        body = item[3]
#        detail = str(Template(file='htmltemplate.tpl', searchList=[{'content': body}]))
        detail = body
#        detail = template.format(detail = item[3])
        print catid, title.encode('utf-8')
        
        fileId = commonlib.crc32unsigned(title)
        
        fileName = output + 'files/' + fileId
        gzip(fileName, detail)
#        gzData = zlib.compress(detail, 6)
#        commonlib.file_put_contents(fileName, gzData)
        smodel.add_story(title, fileId, catid)
    
    cursor.execute("SELECT * FROM vn_gq_udv_category")
    categories = cursor.fetchall()
    for cat in categories:
        smodel.add_category(cat[1], cat[2])
    cursor.close()
    smodel.close()
#    gzipfile(output + 'data.sqlite', output + 'data')
    
    
        


if __name__ == '__main__':
    
    print '> Start'
    convertNewDatabase('/longhoanggiang/database/_cuoitoantap', '/longhoanggiang/truyentext/cuoitoantap')
    print '> Finished'
    os._exit(1)